package com.example.dllo.my36kr.message;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;

import com.example.dllo.lianxi.LianXAdapter;
import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.loading.MySwipeRefreshLayout;
import com.example.dllo.my36kr.news.project.ProjectBean;
import com.example.dllo.my36kr.tools.BaseFragment;
import com.example.dllo.my36kr.tools.NetTool;
import com.example.dllo.my36kr.tools.StaticUrl;

/**
 * Created by dllo on 16/8/15.
 */

/**
 * 消息
 */
public class MessageFragment extends BaseFragment {

    private ListView listView;
    private LianXAdapter adapter;
    private MySwipeRefreshLayout layout;
    int page = 1;

    @Override
    protected int setLayout() {
        return R.layout.fragment_massage;
    }

    @Override
    protected void initView() {
        listView = bindView(R.id.lv_massage);
        adapter = new LianXAdapter(getContext());
        layout = bindView(R.id.msrl_massage);

    }


    @Override
    protected void initData() {
        mNetTool.getNetData(StaticUrl.NEWS_DEPTH_URL, ProjectBean.class, new NetTool.NetListener<ProjectBean>() {
            @Override
            public void onSuccess(ProjectBean projectBean) {
                adapter.setList(projectBean);
                listView.setAdapter(adapter);
                listView.setLayoutAnimation(getListAnim());
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        layout.setOnLoadListener(new MySwipeRefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                page++;
                mNetTool.getNetData("https://rong.36kr.com/api/mobi/news?pageSize=" + (page * 20) + "&columnId=70&pagingAction=up", ProjectBean.class, new NetTool.NetListener<ProjectBean>() {
                    @Override
                    public void onSuccess(ProjectBean projectBean) {
                        adapter.setList(projectBean);
                        listView.setAdapter(adapter);
                        listView.setLayoutAnimation(getListAnim());
                        layout.setLoading(false);
                    }

                    @Override
                    public void onError(String errorMsg) {

                    }
                });
            }
        });

    }
    private LayoutAnimationController getListAnim() {
        AnimationSet set = new AnimationSet(true);
        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(300);
        set.addAnimation(animation);

        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setDuration(500);
        set.addAnimation(animation);
        LayoutAnimationController controller = new LayoutAnimationController(
                set, 0.5f);
        return controller;
    }


}
